package com.tanhua.dubbo.api.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.enums.IdType;
import com.tanhua.dubbo.mapper.QuestionMapper;
import com.tanhua.dubbo.pojo.Question;
import com.tanhua.dubbo.service.IdService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

@DubboService(version = "1.0.0")
public class QuestionApiImpl implements QuestionApi {
    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private IdService idService;

    /**
     * 查询陌生人问题
     *
     * @param userId
     * @return
     */
    @Override
    public Question queryQuestion(Long userId) {
        QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        return this.questionMapper.selectOne(queryWrapper);
    }

    /**
     * 设置陌生人信息
     *
     * @param userId
     * @param content
     * @return
     */
    @Override
    public int setQuestion(Long userId, String content) {

        Question question = this.queryQuestion(userId);
        if (question == null) {
            question.setUserId(userId);
            question.setId(this.idService.createId(IdType.QUESTION));
            question.setCreated(new Date());
            question.setUpdated(question.getCreated());
            if (content==null){
                question.setTxt("你好呀!!!");

                int result = this.questionMapper.insert(question);
            }
            question.setTxt(content);
            int result = this.questionMapper.insert(question);
        }

        if (null==content) {
            return 0;
        }
        question.setTxt(content);
        return this.questionMapper.update(question,new QueryWrapper<Question>().eq("user_id",userId));
    }

}
